package com.qishenyuan.competition.vo;

import com.qishenyuan.competition.model.Competition;
import com.qishenyuan.competition.utils.AliOssUtil;
import com.qishenyuan.competition.utils.DateUtil;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Jacky
 * @since 2025-03-07
 */
@Data
public class CompetitionDetailResp {
    private Integer id;

    private String icon;

    private String title;

    private Integer tag;

    private String publisher;

    private String registrationPeriod;

    private String gamePeriod;

    private String detail;

    private List<String> fileNames;

    private List<String> downloadUrls;

    public static CompetitionDetailResp buildVo(Competition competition){
        CompetitionDetailResp resp = new CompetitionDetailResp();
        resp.setId(competition.getId());
        resp.setIcon(competition.getIcon());
        resp.setTitle(competition.getTitle());
        resp.setTag(competition.getTag());
        resp.setPublisher(competition.getPublisher());
        resp.setRegistrationPeriod(
                DateUtil.format(competition.getRegistrationPeriodBegin(),DateUtil.FORMAT_WITH_SPLIT)+
                "——"+
                DateUtil.format(competition.getRegistrationPeriodEnd(),DateUtil.FORMAT_WITH_SPLIT));
        resp.setGamePeriod(
                DateUtil.format(competition.getGameBeginTime(),DateUtil.FORMAT_WITH_SPLIT)+
                "——"+
                DateUtil.format(competition.getGameEndTime(),DateUtil.FORMAT_WITH_SPLIT));
        resp.setDetail(competition.getDetail());
        List<String> fileNames = new ArrayList<>(Optional.ofNullable(competition.getFileNames())
                .map(CompetitionDetailResp::string2List)
                .orElseGet(Collections::emptyList));
        resp.setFileNames(fileNames);
        List<String> downloadUrls = new ArrayList<>(Optional.ofNullable(competition.getDownloadUrls())
                .map(CompetitionDetailResp::string2List)
                .orElseGet(Collections::emptyList));
        resp.setDownloadUrls(downloadUrls);

        return resp;
    }

    // 把String转换成list，用“;”分隔开
    private static List<String> string2List(String str) {
        List<String> result = Arrays.asList(str.split(";"));
        result = result.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
        return result;
    }
}
